package com.kaichaohulian.baocms.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.activity.BusinessActivity;
import com.kaichaohulian.baocms.activity.GroupChatActivity;
import com.kaichaohulian.baocms.activity.LabelActivity;
import com.kaichaohulian.baocms.activity.NewFriendsActivity;
import com.kaichaohulian.baocms.adapter.ConactAdapter;
import com.kaichaohulian.baocms.app.ActivityUtil;
import com.kaichaohulian.baocms.app.MyApplication;
import com.kaichaohulian.baocms.ecdemo.ui.chatting.ChattingActivity;
import com.kaichaohulian.baocms.ecdemo.ui.chatting.ChattingFragment;
import com.kaichaohulian.baocms.entity.ContactFriendsEntity;
import com.kaichaohulian.baocms.http.HttpUtil;
import com.kaichaohulian.baocms.http.Url;
import com.kaichaohulian.baocms.utils.ChineseToEnglish;
import com.kaichaohulian.baocms.utils.DBLog;
import com.kaichaohulian.baocms.utils.SharedPrefsUtil;
import com.kaichaohulian.baocms.widget.Sidebar;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * Created by Administrator on 2015/12/11.
 */
public class MeFragment extends Fragment {

    private ConactAdapter adapter;
    private List<ContactFriendsEntity> contactList;
    private RelativeLayout re_newfriends, GroupChat, Label, Shopping;
    //            searchFriend;
    private ListView listView;
    private boolean hidden;
    private Sidebar sidebar;
    private ImageView chat;

    private TextView tv_unread;
    private TextView tv_total;
    private LayoutInflater infalter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_me,null);
        contactList = new ArrayList<ContactFriendsEntity>();
        listView = (ListView) view.findViewById(R.id.listHaoYou);
        infalter = LayoutInflater.from(getActivity());
        View headView = infalter.inflate(R.layout.item_contact_list_header, null);
//        searchFriend = (RelativeLayout) headView.findViewById(R.id.rl_contactlist_search);
        chat = (ImageView) headView.findViewById(R.id.img_chat);
        listView.addHeaderView(headView);
        View footerView = infalter.inflate(R.layout.item_contact_list_footer, null);
        listView.addFooterView(footerView);
        re_newfriends = (RelativeLayout) headView.findViewById(R.id.re_newfriends);
        GroupChat = (RelativeLayout) headView.findViewById(R.id.GroupChat);
        Label = (RelativeLayout) headView.findViewById(R.id.Label);
        Shopping = (RelativeLayout) headView.findViewById(R.id.Shopping);

        sidebar = (Sidebar) view.findViewById(R.id.sidebar);
        sidebar.setListView(listView);
//        tv_unread = (TextView) headView.findViewById(R.id.tv_unread);
//        if (((MainActivity) getActivity()).unreadAddressLable.getVisibility() == View.VISIBLE) {
//            tv_unread.setVisibility(View.VISIBLE);
//            tv_unread.setText(((MainActivity) getActivity()).unreadAddressLable.getText());
//        } else {
//            tv_unread.setVisibility(View.GONE);
//        }

        tv_total = (TextView) footerView.findViewById(R.id.tv_total);
        // 设置adapter
        adapter = new ConactAdapter(getActivity(), R.layout.item_contact_list, contactList);
        listView.setAdapter(adapter);

        // 获取设置contactlist
        getContactList();
        initEvent();
        return view;
    }


    public void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (position != 0 && position != contactList.size() + 1) {
                    ContactFriendsEntity user = contactList.get(position - 1);
                    Bundle Bundle = new Bundle();
                    Bundle.putString("userId", user.getPhoneNumber());
                    Bundle.putString("userNick", user.getUsername());
                    Bundle.putString("userAvatar", user.getAvatar());
                    Bundle.putString("imNumber", user.getImNumber());
//                    ActivityUtil.next(getActivity(), ChatActivity.class, Bundle);

                    String remark = user.getRemark();
                    String usernick = "";
                    if (!TextUtils.isEmpty(remark) && !remark.equals("null")) {
                        usernick = user.getRemark();

                    } else {
                        usernick = user.getUsername();

                    }
                    Intent intent = new Intent(getActivity(), ChattingActivity.class);
                    intent.putExtra(ChattingFragment.RECIPIENTS, user.getPhoneNumber());
                    intent.putExtra(ChattingFragment.CONTACT_USER, usernick);
                    intent.putExtra("user_id", user.getId() + "");
                    intent.putExtra(ChattingFragment.CUSTOMER_SERVICE, false);
                    startActivity(intent);
//                    CCPAppManager.startChattingAction(ContactListFragment.this.getActivity()
//                            , user.getPhoneNumber(), user.getUsername(), true);
                }

            }
        });

        re_newfriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.next(getActivity(), NewFriendsActivity.class);
            }
        });
        GroupChat.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityUtil.next(getActivity(), GroupChatActivity.class);
            }
        });
        Label.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.next(getActivity(), LabelActivity.class);
            }
        });
        Shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.next(getActivity(), BusinessActivity.class);
            }
        });
//        searchFriend.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ActivityUtil.next(getActivity(), MyFriendsSearchActivity.class);
//            }
//        });
    }

    private void getContactList() {
        try {

            if (TextUtils.isEmpty(MyApplication.getInstance().UserInfo.getUserId()+"")){

            }else {
                RequestParams params = new RequestParams();
                params.put("id", MyApplication.getInstance().UserInfo.getUserId());
                HttpUtil.post(Url.getFriends, params, new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        try {
                            DBLog.e("获取通讯录好友：", response.toString());
                            if (response.getInt("code") == 0) {
                                contactList.clear();
                                JSONArray JSONArray = response.getJSONArray("dataObject");
                                for (int i = 0; i < JSONArray.length(); i++) {
                                    ContactFriendsEntity contract = new ContactFriendsEntity();
                                    JSONObject jsonObject = JSONArray.getJSONObject(i);
                                    contract.setId(jsonObject.getInt("id"));
                                    contract.setAvatar(jsonObject.getString("avatar"));
                                    contract.setCreatedTime(jsonObject.getString("createdTime"));
                                    contract.setImNumber(jsonObject.getString("imNumber"));
                                    contract.setPhoneNumber(jsonObject.getString("phoneNumber"));
                                    contract.setThermalSignatrue(jsonObject.getString("thermalSignatrue"));
                                    contract.setUsername(jsonObject.getString("username"));
                                    contract.setRemark(jsonObject.optString("remark"));
                                    String name = "";
                                    if (contract.getRemark().equals("null")) {
                                        name = contract.getUsername();
                                    } else {
                                        name = contract.getRemark();
                                    }
                                    Log.d("ContactListFragment", "name.charAt(0):" + name.charAt(0));
                                    if (Character.isDigit(name.charAt(0))) {
                                        contract.setHeader("#");
                                    } else {
                                        contract.setHeader(ChineseToEnglish.getInstance().getSelling(name).trim().substring(0, 1));
                                        char header = contract.getHeader().toLowerCase().charAt(0);
                                        if (header < 'a' || header > 'z') {
                                            contract.setHeader("#");
                                        }

                                    }
                                    boolean flag = false;
                                    for (ContactFriendsEntity tempContact : contactList) {
                                        if (tempContact.getId() == contract.getId()) {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (!flag) {
                                        contactList.add(contract);
                                        SharedPrefsUtil.putValue(getActivity(), contract.getPhoneNumber(), contract.getAvatar() + "-x-" + contract.getUsername());
                                        SharedPrefsUtil.putValue(getActivity(), contract.getId() + "", contract.getAvatar() + "-x-" + contract.getUsername());
                                    }
                                }
                                tv_total.setText(String.valueOf(contactList.size()) + "位联系人");
                                // 对list进行排序
                                Collections.sort(contactList, new PinyinComparator() {
                                });
                                adapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinish() {
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        Toast.makeText(getActivity(), "请求服务器失败", Toast.LENGTH_LONG).show();
                        DBLog.e("tag", statusCode + ":" + responseString);
                    }
                });
            }
        }catch (Exception e){

        }




    }

    public class PinyinComparator implements Comparator<ContactFriendsEntity> {
        @Override
        public int compare(ContactFriendsEntity o1, ContactFriendsEntity o2) {
            String py1 = o1.getHeader();
            String py2 = o2.getHeader();
            // 判断是否为空""
            if (isEmpty(py1) && isEmpty(py2))
                return 0;
            if (isEmpty(py1))
                return -1;
            if (isEmpty(py2))
                return 1;
            String str1 = "";
            String str2 = "";
            try {
                str1 = ((o1.getHeader()).toUpperCase()).substring(0, 1);
                str2 = ((o2.getHeader()).toUpperCase()).substring(0, 1);
            } catch (Exception e) {
                System.out.println("某个str为\" \" 空");
            }
            return str1.compareTo(str2);
        }

        private boolean isEmpty(String str) {
            return "".equals(str.trim());
        }
    }
}
